package com.netty.kkk.controller;

import com.alibaba.fastjson.JSONObject;
import com.netty.kkk.service.ChatService;
import com.netty.kkk.service.UserService;
import com.netty.kkk.vo.ChatInfo;
import com.netty.kkk.vo.Message;
import com.netty.kkk.vo.MessageIndex;
import com.netty.kkk.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint(value = "/userHome/{userId}")
public class WebsocketUserCrl {

    private static UserService userService;
    private static ChatService chatService;

    @Autowired
    public void setMyServiceImpl(UserService userService){
        WebsocketUserCrl.userService = userService;
    }
    @Autowired
    public void setMyChatImpl(ChatService chatService){
        WebsocketUserCrl.chatService = chatService;
    }

    public static int CreateNewChat_Type = 0;//新建群聊 类型
    public static int ChangeUserInfo_Type = 1;//修改信息 类型

    //存放已登录用户
    public static ConcurrentHashMap<String, Session> onHomeUserMap = new ConcurrentHashMap<>();

    /**
     * 用户上线
     * @param session
     * @param userId
     */
    @OnOpen
    public void onOpen(Session session, @PathParam("userId") String userId){
        System.out.println(userId+"登录首页");
        onHomeUserMap.put(userId, session);
    }

    /**
     * 用户离线
     * @param session
     * @param userId
     */
    @OnClose
    public void onClose(Session session, @PathParam("userId") String userId){
        System.out.println(userId+"退出首页");
        onHomeUserMap.remove(userId);
    }

    /**
     * 新建群聊，发送消息给当前在线的群聊成员，在侧边栏群聊列表中生成群聊信息（离线成员上线时会自动获取新群聊信息）
     * @param memberList
     * @param chatInfo
     */
    public void createNewGroupChat(List<String> memberList, ChatInfo chatInfo){
        MessageIndex messageIndex = new MessageIndex();
        messageIndex.setMessageType(CreateNewChat_Type);
        messageIndex.setMessage(chatInfo);
        try{
            for(Map.Entry<String, Session> entry : onHomeUserMap.entrySet()){
                if(memberList.contains(entry.getKey())){
                    String text = JSONObject.toJSONString(messageIndex);
                    entry.getValue().getBasicRemote().sendText(text);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void changeUserInfoMsg(UserInfo userInfo){
        MessageIndex messageIndex = new MessageIndex();
        messageIndex.setMessageType(ChangeUserInfo_Type);
        messageIndex.setMessage(userInfo);
        try{
            for(Map.Entry<String, Session> entry : onHomeUserMap.entrySet()){
                String text = JSONObject.toJSONString(messageIndex);
                entry.getValue().getBasicRemote().sendText(text);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
